var a = getApp();

Page({
    data: {
        ad: {}
    },
    onShow(){
        this.setData({
            ad: ''
        });
        this.setData({
            ad: getApp().globalData.selfAd[parseInt(getApp().globalData.selfAd.length*Math.random())]
        });
    },
    onLoad: function(t) {
        var e = this;
        wx.getImageInfo({
            src: a.globalData.bgPic,
            success: function(a) {
                e.bgPic = a.path, e.draw();
            }
        });
    },
    onReady: function() {},
    draw: function() {
        var t = a.globalData.scale, e = a.globalData.rotate, n = a.globalData.hat_center_x, o = a.globalData.hat_center_y, s = a.globalData.currentHatId, c = wx.createCanvasContext("myCanvas"), i = wx.getSystemInfoSync().windowWidth, l = 100 * t;
        c.clearRect(0, 0, i, 300), c.drawImage(this.bgPic, i / 2 - 150, 0, 300, 300), c.translate(n, o), 
        c.rotate(e * Math.PI / 180), c.drawImage("/image/" + s + ".png", -l / 2, -l / 2, l, l), 
        c.draw();
    },
    savePic: function() {
        var t = wx.getSystemInfoSync().windowWidth;
        wx.canvasToTempFilePath({
            x: t / 2 - 150,
            y: 0,
            height: 300,
            width: 300,
            canvasId: "myCanvas",
            success: function(t) {
                a.globalData.successPic = t.tempFilePath, wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath,
                    success: function(a) {
                        wx.showToast({
                            title: "保存成功"
                        });
                    },
                    fail: function(a) {
                        console.log("err:" + a);
                    }
                });
            }
        });
    },
    onShareAppMessage: function() {
        return {
            title: "口罩头像-图片编辑助手",
            path: "pages/mask/index/index"
        };
    }
});